package com.application.fabric.fabriccontrol.operate.controlfabric;

import com.application.fabric.fabriccontrol.existevidence.CreatePDF;
import com.application.fabric.fabriccontrol.operate.entityfabric.BasicAsset;
import com.application.fabric.fabriccontrol.operate.entityfabric.Trademsg;
import com.application.fabric.fabriccontrol.operate.mapper.Assistmapper;
import com.application.fabric.fabriccontrol.operate.serverfabric.HDFSservice;
import com.application.fabric.fabriccontrol.operate.serverfabric.MysqlFabricServer;
import com.application.fabric.fabriccontrol.operate.serverfabric.ServeAsset;
import com.application.fabric.users.config.Dealmsg;
import com.application.fabric.users.config.encryptAl.Hashencry;
import com.application.fabric.users.entity.Evidence;
import com.application.fabric.users.entity.ExtractToken;
import com.application.fabric.users.entity.Returnmsg;
import com.cloudant.client.internal.util.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*对请求的接受 简单版本
* */
@RestController
@RequestMapping("/operate")
public class Controlfabric {
    @Autowired
    private ServeAsset serveAsset;
    @Resource
    public ExtractToken token;
    @Autowired
    private CreatePDF createPDF;

//   查询账本全部资产
    @GetMapping("queryallasset")
    public Map<String,Object> QueryAllAsset() throws Exception{
        System.out.println("--------------------------");
        System.out.println(token.getPhonenum());
        System.out.println("--------------------------");
        String result = serveAsset.Queryallasset();

        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","查询成功");
        map.put("data",result);
        return map;
    }

//    id查询 可以作为开放网络查询
    @GetMapping("/querybyassetid/{assetid}")
    public Map<String,Object> QueryByAssetId(@PathVariable("assetid") String assetid) throws Exception{
//        先做异常处理 就是判断资产在不在
//        在的化进行查询 不再的化返回资产查询为空
        Map<String,Object> map = new HashMap<>();
        String result="";
        String know=serveAsset.Knowexit(assetid);
        if (know.equals("true")){
//             result = serveAsset.Queryassetbyid(assetid);
             List<BasicAsset> qu = mysqlFabricServer.queryassetbyid(assetid);
            System.out.println(qu.get(0));
            result = String.valueOf(qu.get(0));
        }


        map.put("code",200);
        map.put("msg","查询成功");
        map.put("data",result);
        return map;
    }

//    判断Assetid是否存在
    @GetMapping("knowexit")
    public Map<String,Object> Knowexit(@RequestParam("assetid") String assetId) throws Exception{
        String result = serveAsset.Knowexit(assetId);
        System.out.println(assetId);
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","查询成功");
        map.put("data",result);
        return map;
    }

//    交易资产
    @PostMapping("tradingassets")
    public Map<String,Object> TradingAssets(@RequestBody Trademsg trademsg) throws Exception{

        System.out.println(trademsg.getAssetId());
        System.out.println(trademsg.getBuyer());
        String result = serveAsset.TransferAsset(trademsg.getAssetId(),trademsg.getBuyer());
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","转让成功");
        map.put("data",result);
        return map;
    }

//    创建资产 id前端传递

    @Autowired
    private MysqlFabricServer mysqlFabricServer;
    @Resource
    private ExtractToken extractToken;
    @Autowired
    private Hashencry hashencry;
    @PostMapping("submitallasset")
    public Map<String,Object> Submitallasset(BasicAsset basicAsset) throws Exception{
        System.out.println("打印主体地址"+basicAsset.getFile());
        System.out.println("受到请求");
        System.out.println(token.getPhonenum());
        Map<String,Object> map = new HashMap<>();
//            id设置
            String idset= hashencry.hash(token.getPhonenum(),Long.toString(System.currentTimeMillis()));
            basicAsset.setId(idset);
//            拥有着设置
             basicAsset.setOwner(token.getUserid());

        System.out.println("==================================");
        System.out.println(basicAsset.getId()+"................"+basicAsset.getName()+"................"+basicAsset.getCollections()+"................"+basicAsset.getOwner()+"................"+basicAsset.getAppraisedvalue());
//             进行提交
            String result = serveAsset.Submitallasset(basicAsset);
//            数据库辅助存储
//             数据填充
             Date date= new Date();
             basicAsset.setTime(date);
             basicAsset.setUserid(extractToken.getUserid());

            if (result.equals("")){
                int res= mysqlFabricServer.StorageServer(basicAsset);
                System.out.println("===================================================");
                System.out.println(res);
                System.out.println("===================================================");
//                分布式文件存储
                hdfSservice.issueservice(basicAsset,basicAsset.getName());
            }

            map.put("code",200);
            map.put("msg","资产创建成功");
            map.put("data","result");
            return map;

    }

//    删除资产
    @DeleteMapping("deleteasset")
    public Map<String,Object> Deleteasset(@RequestParam("assetid") String assetId) throws Exception {
        String result = serveAsset.Deleteasset(assetId);
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","资产删除成功");
        map.put("data",result);
        return map;
    }

//资产交易历史数据
    @GetMapping("gethistorydata")
    public Map<String,Object> GetHistorydata(@RequestParam("assetid") String assetId) throws Exception{
        String result = serveAsset.Gethistorydata(assetId);
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","资产历史数据查询成功");
        map.put("data",result);
        return map;
    }

    @GetMapping("assetsbyowner")
    public Map<String,Object> GetAssetByOwner(@RequestParam("owner") String owner) throws Exception {

        String result = serveAsset.getQueryResultForQueryString(owner);
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","资产历史数据查询成功");
        map.put("data",result);
        return map;
    }

    @GetMapping("assetsbystring")
    public Map<String,Object> assetsbystring(@RequestParam("owner") String owner) throws Exception {

        String result = serveAsset.QueryassetbyOwner(owner);
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","资产历史数据查询成功");
        map.put("data","result");
        return map;
    }

//    临时测试接口
@Resource
private Returnmsg returnmsg;

    @Autowired
    private Dealmsg dealmsg;
    @Autowired
    private HDFSservice hdfSservice;



    @GetMapping("linshi")
    public Map<String,Object> linshi() throws Exception{

        createPDF.writehdfs();
        returnmsg.setData("成功");
        returnmsg.setCode(200);
        return  dealmsg.successmsg(returnmsg);
    }
}
